import { _decorator, Component, assetManager, Prefab, director, instantiate, Node  } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('Main') // 用 @ccclass 声明类为 Cocos 组件
export class Main extends Component {

    onLoad() {
        // 加载具体资源
        assetManager.loadBundle('mahjong3d', (err, bundle) => {
            if (err) {
                console.error('Failed to load bundle:', err);
                return;
            }
            console.log('Bundle loaded:', bundle);
        });
    }

    start() {
        // 加载 Asset Bundle
        assetManager.loadBundle('mahjong3d', (err, bundle) => {
            if (err) {
                console.error('Failed to load bundle:', err);
                return;
            }
            console.log('Bundle loaded:', bundle);

            // 加载具体资源
            bundle.load('Scenes/3DGame', Prefab, (err, prefab) => {
                if (err) {
                    console.error('Failed to load prefab:', err);
                    return;
                }
                console.log('Prefab loaded:', prefab);

                const scene: any = director.getScene();
                let node3d = scene.getChildByName('3DSceneRoot');
                if(node3d == null)
                {
                    node3d = new Node();
                    node3d.name = '3DSceneRoot'; // 设置节点名称
                    scene.addChild(node3d);
                }
		        node3d.active = true;

                const prefabInstance = instantiate(prefab);
                node3d.addChild(prefabInstance); // 将实例化的 prefab 添加到 "ParentNode" 下

                console.log('Prefab successfully loaded and added to the scene!');
            });
        });
    }

}